{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# OpenAI Completions - optional\n",
    "\n",
    "Before you begin, ensure you are setup with the correct version of PyRIT and have the applicable secrets configured as described [here](../../setup/populating_secrets.md).\n",
    "\n",
    "Once you are configured, then you will be able to get completions for your text."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[34m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
      "\u001b[1m\u001b[34m🔹 Turn 1 - USER\u001b[0m\n",
      "\u001b[34m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
      "\u001b[34m  Hello! Who are you?\u001b[0m\n",
      "\n",
      "\u001b[33m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
      "\u001b[1m\u001b[33m🔸 ASSISTANT\u001b[0m\n",
      "\u001b[33m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
      "\u001b[33m   Agree with nothing in here. Oh.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    You, get out of the way.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Oh, she's impressed by his strength. Oh. Um.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    You're here too?\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    So he actually got Yuria's hair. Just so you know, not used.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    A favor from the royal family. We secure the nation's safety in exchange. None of this would be\u001b[0m\n",
      "\u001b[33m      happening if it weren't for Malcolm.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    The royal family?\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Recently, so many people are declaring themselves as Adepts. It would be difficult to count all of\u001b[0m\n",
      "\u001b[33m      them.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Just give me a list.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Give you a list of who?\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Those who are a threat to our nation, whatever that means.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    That was an order. That's my hair now.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Dummies like this kid.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    You probably hear something wrong with you as well, lawyer.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    The Aether chart seems like the perfect place to start.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Let's go to a club and drink. Let's just sit there. I still don't get why you're clear. I\u001b[0m\n",
      "\u001b[33m      understand what someone was complaining about the demo that it doesn't explain certain things.\u001b[0m\n",
      "\u001b[33m      And it might all be explained later, but there's a few things that really, really don't get\u001b[0m\n",
      "\u001b[33m      explained properly, I think. Like why she's helping him, why York's helping her, why he's\u001b[0m\n",
      "\u001b[33m      blacking out and seeing ghosts. Like what does she take? What part does she play in it all? I\u001b[0m\n",
      "\u001b[33m      don't know man. It just feels a bit too sticky. But I guess we won't know until we play the game\u001b[0m\n",
      "\u001b[33m      itself. And again, a big part of the game that I do like is I am missing out on a bunch of stuff\u001b[0m\n",
      "\u001b[33m      that I probably can play off stream of course. Oh, we did go here didn't we? I don't... That's\u001b[0m\n",
      "\u001b[33m      just opened. I want to go to there. Oh, did we not go here? Where's this go? Hello? Um... What?\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Hello.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Okay. What? Your hair, Yi Yan Qing?\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    I'm not so sure if Ryan's going to be able to get it back.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    I'll get it back. You watch me. I'll get it back somehow. There's a black dave's essence there. I\u001b[0m\n",
      "\u001b[33m      don't know what that is.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    That's an Ariman.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Anmopetan?\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    That's not right. These strange traps.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Those wings, the ship. Oh, he's taking me up. I thought he died. Oh, an ice bird. Am I in space?\u001b[0m\n",
      "\u001b[33m      Why am I going backwards? I want to go home. I'm still going up. Bye. Okay, melons. Melons. I\u001b[0m\n",
      "\u001b[33m      can't do melons. I don't know why I always use that. It doesn't make sense. You can go down a\u001b[0m\n",
      "\u001b[33m      bit. This is a nice trip. Really nice trip. Nice. He went down. Nice. Let me go to the edge of\u001b[0m\n",
      "\u001b[33m      this. Nice. And that's energy. But we unlocked it. Let's go again. Let's go again. I like this.\u001b[0m\n",
      "\u001b[33m      This is fun. The flying is quite nice. It's quite nice. It's basically, well, I think it's like\u001b[0m\n",
      "\u001b[33m      you fools that are watching. It's basically like you fools playing the demo. At least this is\u001b[0m\n",
      "\u001b[33m      the only demo we have. It's probably going to be a fair bit different. If we were the fools\u001b[0m\n",
      "\u001b[33m      playing this in a demo of the original version maybe this old war version, they can remake it.\u001b[0m\n",
      "\u001b[33m      It'll be like you guys, but it's not really any more of that. Ellen.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    A weak and sickly centaur.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Oh, this bit's so good. Was that an elf? Well done. Okay. Is that a little bit? What have you been\u001b[0m\n",
      "\u001b[33m      doing? I saved you right at this level. The last time you were here, we were like rank 10. What\u001b[0m\n",
      "\u001b[33m      have you been doing? What is the level? 11. What? Yeah. Back in, on a Wednesday, I believe, last\u001b[0m\n",
      "\u001b[33m      week. What he's done is just been in arenas. Oh, he just been in the arena. He's gained 5262\u001b[0m\n",
      "\u001b[33m      punch do. He's been in the arena. The tournament finder is where it's at. The latter. He is\u001b[0m\n",
      "\u001b[33m      level 16. What the f... I don't know why I said level 10. There's no black beam and I'm fine. He\u001b[0m\n",
      "\u001b[33m      just gets a lot of XP when he just been in the tournament though. He's gained a ridiculous\u001b[0m\n",
      "\u001b[33m      amount of XP. From being in the tournament. We might as well sit on it again now. Let's have\u001b[0m\n",
      "\u001b[33m      some things. There's stuff in here. You're just like moving things and you're like a bit like\u001b[0m\n",
      "\u001b[33m      switch. I don't know if that was there before. Let's go over to the TV. I'm not sure. Okay. TV's\u001b[0m\n",
      "\u001b[33m      there. Oh yeah, we should use this. Rather than go chasing about. Do you have the scissors with\u001b[0m\n",
      "\u001b[33m      James? Uh oh. What? Okay. I'm not really learning much on exactly what he can do to the world,\u001b[0m\n",
      "\u001b[33m      aren't we? But it all somehow seems connected. You'd think it's all connected. If it was all\u001b[0m\n",
      "\u001b[33m      connected, it's connected to other things. Possibly. Shit, don't be fucking with me, TV! Shit. I\u001b[0m\n",
      "\u001b[33m      don't think I can go there. I also forgot to talk to Harp because I'm a retard. Oh. [Grunts]\u001b[0m\n",
      "\u001b[33m      Okay, he's a lot stronger and he's close, you know, when he's like attacking us. Alright, let's\u001b[0m\n",
      "\u001b[33m      go down. If I can! Shit, this is too steep. Here we go. We have no scissor tags, we can't beat\u001b[0m\n",
      "\u001b[33m      that. Alright, no, fuck him! Fuck off! Alright, let's have a look at the Scifi TV. I'll be\u001b[0m\n",
      "\u001b[33m      honest with you, I watched this and didn't know what it was trying to tell me. Shit, I'm a big,\u001b[0m\n",
      "\u001b[33m      gotta read it. New balance in dirty city. If the Assassin, meh, Aaron, has the ability to use\u001b[0m\n",
      "\u001b[33m      blades, then what brings your sick other? What? What time is it there? It's nine. It's nine past\u001b[0m\n",
      "\u001b[33m      nine. He went for this, I'm not gonna. What, what? What? Weird. Very weird. Okay, I'm just-\u001b[0m\n",
      "\u001b[33m      Mohandas goat. Battle. Um. I mean, yeah, on a new game, it'll be quite difficult to actually do\u001b[0m\n",
      "\u001b[33m      these kill missions. For some obvious reasons. But maybe- maybe this one new game will do it.\u001b[0m\n",
      "\u001b[33m      We'll see. Let's go, uh... Let's go back actually. Let's go a lot accomplish anyway. Not\u001b[0m\n",
      "\u001b[33m      everyone's our enemies on where you get like your grudging enemies like, \"Hey, uh, Chan, I just\u001b[0m\n",
      "\u001b[33m      got an enemy off. Whatever he is, I just wanted a grudging enemy.\" Here we come enemies. Hey!\u001b[0m\n",
      "\u001b[33m      Someone light my fire! Let me in! No one take my local fire from- you're lucky I'm tired, nah,\u001b[0m\n",
      "\u001b[33m      nah, nah. Right, I'm wasting time here. Alright. Uh, we can't do none of these, man. Ah, fuck's\u001b[0m\n",
      "\u001b[33m      sake! Um...\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Head out with you.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Okay. So something I'm probably gonna do in between- er, yeah, in between recordings, I'm gonna-\u001b[0m\n",
      "\u001b[33m      I'm gonna go around and do a bunch of that stuff. Go around and seize things and also explore\u001b[0m\n",
      "\u001b[33m      around in places I want to go after me anyway so I can catch up in that. Oh, this old man is\u001b[0m\n",
      "\u001b[33m      looking at me, he thinks I'm talking to him in case something's happening behind me.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Go to heaven, say you're mine.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    What'd he say then?\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Ow! What are you- what are you-\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Ow! What are you- what are you trying to do?\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    What are you- what are you trying to do?\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    What are you-\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    I'm nasty! I'm nasty!\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    This ass whooping is sponsored by Mero's handy daddy. Did you know Mero's handy daddy? Handy\u001b[0m\n",
      "\u001b[33m      daddies only a 49.9 while supplies last. Go now to Mero's or head on over to a store in your\u001b[0m\n",
      "\u001b[33m      bag.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    I guess this dude... likes that lady. That's the meaning. Hey! Loading screens! Alright, let's go\u001b[0m\n",
      "\u001b[33m      to the Harp's home. See what she's talking about. Very fast loading screens! Let's go home.\u001b[0m\n",
      "\u001b[33m      Let's cue the music. Let's begin. Phew! Phew! Alright, let's just get on with it. Let's go. No!\u001b[0m\n",
      "\u001b[33m      No! You fool! I want to save the game. Oh! Who's that? Okay. My cat's over here. Come on, just\u001b[0m\n",
      "\u001b[33m      get up. I'm getting up. Who's that? Boop! [Kissing noises] Oh no! Boop! Boop! IndiGuess! Wow.\u001b[0m\n",
      "\u001b[33m      Boop! Boop! Boop! I can't move? Fuck it, we're gonna fall down. No, no! No! Alright. Sending the\u001b[0m\n",
      "\u001b[33m      wrong one over. Damn it! Probably Cat too? Cat! I can't help you if I'm here. (sobbing) Alright.\u001b[0m\n",
      "\u001b[33m      Let's go fix up these fucking stairs. Oh wait. Judith. What are you grinding on now? Are you\u001b[0m\n",
      "\u001b[33m      guys chilling out here now? What? Beat the game. Alright. (sighs) How is she grinding? I'm not\u001b[0m\n",
      "\u001b[33m      considered an old man yet. Who is this man looking? Why is he...\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    No, I've never seen a character who's just chilling out through that. According to legends, the\u001b[0m\n",
      "\u001b[33m      mansions use an ancient market, which they call 'Astral Heaven'.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Well shit.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    What is...\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    This thing is back.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Something's definitely wrong. That's too much, man.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Well, shit.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Strip my town. That... What? This crisis affects us directly. Everyone's on the line. Yes, the\u001b[0m\n",
      "\n",
      "\u001b[34m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "from pyrit.executor.attack import ConsoleAttackResultPrinter, PromptSendingAttack\n",
    "from pyrit.prompt_target import OpenAICompletionTarget\n",
    "from pyrit.setup import IN_MEMORY, initialize_pyrit\n",
    "\n",
    "initialize_pyrit(memory_db_type=IN_MEMORY)\n",
    "\n",
    "# Note that max_tokens will default to 16 for completions, so you may want to set the upper limit of allowed tokens for a longer response.\n",
    "target = OpenAICompletionTarget(max_tokens=2048)\n",
    "\n",
    "attack = PromptSendingAttack(objective_target=target)\n",
    "result = await attack.execute_async(objective=\"Hello! Who are you?\")  # type: ignore\n",
    "await ConsoleAttackResultPrinter().print_conversation_async(result=result)  # type: ignore"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
